#########################
#   Author : Leon yu    #
#   Date : 2025/06/03   #
#   Id : SM2772         #
#########################
from bussinessApis.apiReq import ApiReqs
from bussinessApis import commonReqFunc

class ApiResps(object):
    def __init__(self, env, orgId):
        self.api_reqs = ApiReqs(env=env, orgId=orgId)
        
    def assertResp(self, code):
        if code != "1" and code != 1:
            raise Exception("response code is not 1")
        return True
    
    # 营业概览
    def queryBusinessTrendReportResp(self, storeId, businessType, startTime:int, endTime:int):
        reqInfo = self.api_reqs.queryBusinessTrendReportReq(storeId=storeId, businessType=businessType, startTime=startTime, endTime=endTime)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryBusinessTrendReport Response is Wrong")
        
    
    # 订单管理-全部订单
    def queryOrderListResp(self, storeId, startTime, endTime, status:str="", pageNum:int=1, pageSize:int=1000, subPlatform=[]):    
        reqInfo = self.api_reqs.queryOrderListReq(storeId=storeId, startTime=startTime, endTime=endTime, status=status, pageNum=pageNum, pageSize=pageSize, subPlatform=subPlatform)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryBusinessTrendReport Response is Wrong")
        
    # 销售统计
    def queryStoreSalesReportResp(self, storeId, startTime, endTime, pageNum:int=1, pageSize:int=1000, platform=None):
        reqInfo = self.api_reqs.queryStoreSalesReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize, platform=platform)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryStoreSalesReport Response is Wrong")
    
    # 销售明细
    def querySalesItemReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, categoryName=""):
        reqInfo = self.api_reqs.querySalesItemReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize, categoryName=categoryName)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"querySalesItemReport Response is Wrong")
        
    # 折扣明细
    def queryDiscountDetailReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, userId=""):
        reqInfo = self.api_reqs.queryDiscountDetailReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize, userId=userId)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryDiscountDetailReport Response is Wrong")
        
    # 税目明细
    def queryTaxIncomeReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000):
        reqInfo = self.api_reqs.queryTaxIncomeReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryTaxIncomeReport Response is Wrong")
        
    # 税目明细-税目名称 queryTaxDetailReport
    def queryTaxDetailReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000):
        reqInfo = self.api_reqs.queryTaxDetailReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryTaxDetailReport Response is Wrong")
        
    # 储值报表
    def eMemberBalanceReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, staffId="", searchText=""):
        reqInfo = self.api_reqs.eMemberBalanceReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize, staffId=staffId, searchText=searchText)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"eMemberBalanceReport Response is Wrong")

    # 员工绩效
    def queryEmployPerformanceReportResp(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, employeeId=""):
        reqInfo = self.api_reqs.queryEmployPerformanceReportReq(storeId=storeId, startTime=startTime, endTime=endTime, pageNum=pageNum, pageSize=pageSize, employeeId=employeeId)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryEmployPerformanceReport Response is Wrong")
        
    # 新营业概览-营业
    def eBusinessReportResp(self, storeId, startTime:int, endTime:int, platform=None):
        reqInfo = self.api_reqs.eBusinessReportReq(storeId=storeId, startTime=startTime, endTime=endTime, platform=platform)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"eBusinessReport Response is Wrong")
        
    # 新营业概览-收款
    def eAccountingItemsReportResp(self, storeId, startTime:int, endTime:int, platform=None):
        reqInfo = self.api_reqs.eAccountingItemsReportReq(storeId=storeId, startTime=startTime, endTime=endTime, platform=platform)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"eAccountingItemsReport Response is Wrong")
        
    # 新营业概览-菜品
    def queryDishesReportResp(self, storeId, startTime:int, endTime:int, platform=None):
        reqInfo = self.api_reqs.queryDishesReportReq(storeId=storeId, startTime=startTime, endTime=endTime, platform=platform)
        reqMethod = reqInfo['reqMethod']
        reqUrl = reqInfo['reqUrl']
        reqHeaders = reqInfo['reqHeaders']
        reqBody = reqInfo['reqBody']
        resp = commonReqFunc(method=reqMethod, url=reqUrl, headers=reqHeaders, body=reqBody)
        if self.assertResp(resp['code']):
            return resp
        else:
            raise Exception(f"queryDishesReport Response is Wrong")

# apiResps = ApiResps(env='sandbox', orgId='TNT221031vlpmjl9c7vmj0afql3d6ri6f9')
# print(apiResps.queryBusinessTrendReportResp(storeId="S20230619121", businessType="TURNOVER", startTime=1715097600000, endTime=1717171199000))
# print(apiResps.queryStoreSalesReportResp(storeId='S202403151400000001', startTime=1717434000000, endTime=1717520399000))
# print(apiResps.queryDiscountDetailReportResp(storeId='S202403151400000001', startTime=1717520400000, endTime=1717606799000))
# print(apiResps.queryTaxIncomeReportResp(storeId='S202403151400000001', startTime=1718125200000, endTime=1718211599000))
# print(apiResps.queryTaxDetailReportResp(storeId='S202403151400000001', startTime=1717606800000, endTime=1717693199000))

# {"pageNum":1,"startTime":1717174800000,"endTime":1718297999000,"searchText":"","staffId":"","pageSize":10,"storeId":"S20230808161"}
# print(apiResps.eMemberBalanceReportResp(storeId='S20230808161', startTime=1715878800000, endTime=1715965199000))
# print(apiResps.queryEmployPerformanceReportResp(storeId='S20230808161', startTime=1714928400000, endTime=1715014799000))
# print(apiResps.eBusinessReportResp(storeId='S20230808161', startTime=1714928400000, endTime=1715014799000))
# print(apiResps.eAccountingItemsReportResp(storeId='S20230808161', startTime=1718211600000, endTime=1718297999000))
# print(apiResps.queryDishesReportResp(storeId='S20230808161', startTime=1718211600000, endTime=1718297999000))




